#include "towerTank.h"

#define GROUND 600-285
#define IMAGE "tank1.png"
#define IMAGE1 "startfire.png"

#define TANK_MOVE	0
#define TANK_FIRE	1
#define TANK_FIRE1	2

CtowerTank::CtowerTank()
{

}

CtowerTank::CtowerTank(LPD3DXSPRITE spriteHandler, float Scaling, CQuadTree* q)
{
	m_qroot = q;
	m_nId = 3;
	m_nAndr = 1700;
	m_ntankIndex = 0;
	m_nposX = 0.0f;
	m_nposY = GROUND;
	
	timeBetweenUpdated = 90;

	m_fverX = 0.0f;
	m_fverY = 0.0f;
	m_fframeWidth = 100.0f;
	m_fframeHeight = 100.0f;
	m_fscaling = Scaling;

	m_dwlastUpdate = 0;

	m_Sprite = new CSprite();
	_fire = new CSprite();

	bullet1 = new CPistol(spriteHandler, m_fscaling);

	_shells = new CPistol(spriteHandler, m_fscaling);
	//constructor
	loadSprite(spriteHandler);
	setRect();
	tankFire();
	//moveTank();
}

CtowerTank::~CtowerTank()
{
	m_Sprite = NULL;
}

void CtowerTank::loadSprite(LPD3DXSPRITE spritehandler)
{
	m_Sprite->CreateSprite(spritehandler, IMAGE, m_fframeWidth, m_fframeHeight, 9, 30, m_fscaling, D3DCOLOR_XRGB(255, 255, 255));
	_fire->CreateSprite(spritehandler, IMAGE1, 50, 50, 17, 17, m_fscaling, D3DCOLOR_XRGB(255, 255, 255));
}

void CtowerTank::update(int DeltaTime, int moveCame)
{
	m_nAndr -= m_fverX;
	m_nposX = m_nAndr - moveCame;
	m_nposY += (int) m_fverY;

	if (bullet1->m_nposY < GROUND+160)
		bullet1->Update(DeltaTime, moveCame);

	if (_shells->m_nposY < GROUND+160)
		_shells->Update(DeltaTime, moveCame);

	DWORD TimeNow = GetTickCount();

	if (TimeNow - m_dwlastUpdate > timeBetweenUpdated)
	{
		next();
		m_dwlastUpdate = TimeNow;
	}

	setRect();
}

void CtowerTank::render()
{
	m_Sprite->Render(m_nposX, m_nposY);

	if (m_ntankIndex != TANK_MOVE)
		_fire->Render(m_nposX - 50, m_nposY);
	if (bullet1->m_nposY > GROUND+160)
	{
		bullet1->m_nposY = bullet1->m_nposY - 285;
		bullet1->smock();
	}

	if(!bullet1->m_breleaseSmock)
		bullet1->Render();

	if (_shells->m_nposY < GROUND+160)
		_shells->Render();
}

void CtowerTank::next()
{
	m_Sprite->Next();
	_fire->Next();

	if( m_Sprite->GetIndex() == 2 && m_ntankIndex == TANK_MOVE)
		m_Sprite->SetIndex(0);

	if( m_Sprite->GetIndex() == 8 && _fire->GetIndex() == 8 && m_ntankIndex == TANK_FIRE)
		tankFire1();

	if(m_Sprite->GetIndex() == 8 && _fire->GetIndex() == 16 && m_ntankIndex == TANK_FIRE1)
	{
		moveTank();
	}
}

void CtowerTank::setRect()
{
	m_ObRect = new RECT();
	m_ObRect->left = m_nAndr + 60;
	m_ObRect->right = m_ObRect->left + m_fframeWidth- 20;
	m_ObRect->top = m_nposY;
	m_ObRect->bottom = m_ObRect->top + m_fframeHeight - 5;
}

void CtowerTank::moveTank()
{
	m_fverX = 0.0f;
	m_ntankIndex = TANK_MOVE;
	m_Sprite->SetIndex(0);
	m_Sprite->SetSpriteLine(0);
}

void CtowerTank::tankFire()
{
	m_ntankIndex = TANK_FIRE;
	_fire->SetIndex(0);
	m_Sprite->SetIndex(0);
	m_Sprite->SetSpriteLine(1);
	m_fverX = 0.0f;
	bullet1->m_nAndr = m_nAndr +20;
	bullet1->m_nposY = m_nposY + 30;
	bullet1->bulletTank();
}

void CtowerTank::tankFire1()
{
	m_fverX = 0.0f;
	m_ntankIndex = TANK_FIRE1;
	_fire->SetIndex(8);
	m_Sprite->SetIndex(0);
	m_Sprite->SetSpriteLine(2);

	bullet1->fire();

	_shells->m_nAndr = m_nAndr + 140;
	_shells->m_nposY = m_nposY + 10;
	_shells->shells();
}